package concurrent;

/**
 * @Author: 18362
 * @Create: 2022-09-14 16:30:18 星期三
 */
public class DeadLock {

    static class MyThread extends Thread {

        private Object own;
        private Object other;

        public MyThread(String name, Object own, Object other) {
            super(name);
            this.own = own;
            this.other = other;
        }

        @Override
        public void run() {
            synchronized (own) {
                System.out.println(getName() + "获取自己的锁成功: " + own);
                try {
                    Thread.sleep(3000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                synchronized (other) {
                    System.out.println(getName() + "获取其他锁成功: " + other);
                }
                System.out.println(getName() + "释放了其他锁: " + other);
            }
            System.out.println(getName() + "释放了自己的锁: " + own);
        }
    }

    public static void main(String[] args) {
        System.out.println("演示死锁");
        Object obj1 = new Object(), obj2 = new Object();
        new MyThread("thread-A", obj1, obj2).start();
        new MyThread("thread-B", obj2, obj1).start();
    }
}
